package com.zhaokuner.parentstarter.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Locale;

/**
 * 类描述:获取ApplicationContext
 *
 * @author 赵希坤 zhaoxikun
 * @date 2020/12/19 10:57
 */
public class MyAppContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext acx) throws BeansException {
        MyAppContextUtil.applicationContext = acx;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        return (T) MyAppContextUtil.applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> clazz) {
        return MyAppContextUtil.applicationContext.getBean(clazz);
    }

    public static String getMessage(String code, Object[] args, Locale locale) {
        return applicationContext.getMessage(code, args, locale);
    }

}
